package com.joplus.housekeeper.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.FlightListBean;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;


/**
 * author： ccm
 * time： 2017/6/5.
 * description：
 */

public class FlightListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private final LayoutInflater layout;
    LinkedHashMap<String, List<FlightListBean.DataBean.AirportsBean>> airports;
    private final ArrayList<String> keys;

    public FlightListAdapter(Context context, LinkedHashMap airports, ArrayList<String> keys) {
        this.mContext = context;
        this.airports = airports;
        layout = LayoutInflater.from(mContext);
        this.keys = keys;

    }

    @Override
    public int getGroupCount() {
        return keys.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        if (groupPosition >= keys.size()) {
            return 0;
        }
        return airports.get(keys.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupPosition;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = layout.inflate(R.layout.item_flight_f, null);
        TextView tv = (TextView) view.findViewById(R.id.tv);

        tv.setText(keys.get(groupPosition));
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = layout.inflate(R.layout.item_flight_c, null);
        TextView tv1 = (TextView) view.findViewById(R.id.tv1);
        TextView tv2 = (TextView) view.findViewById(R.id.tv2);

        FlightListBean.DataBean.AirportsBean airportsBean = airports.get(keys.get(groupPosition)).get(childPosition);
        tv1.setText(airportsBean.getName());
        tv2.setText(airportsBean.getCode());
        return view;
    }


}
